package cn.sfa.controller.affix;


import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.configuration.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sfa.common.utils.ConfigUtil;
import cn.sfa.common.utils.MajorKeyGenerate;
import cn.sfa.common.utils.WebInitBinderContoller;
import cn.sfa.entity.affixItem.SaleAffixItem;
import cn.sfa.service.affix.AffixService;

/**
 *@author 周友平
 *@version 创建时间：2012-4-5下午05:18:17
 *类说明
 */
@Controller
@RequestMapping("affix/index")
public class AffixController  extends WebInitBinderContoller {

	@Autowired 
	private AffixService affixService;
	
	@Autowired
	MajorKeyGenerate majorKeyGenerate;
	
	
	/**
	 * 
	 * 通用上传
	 * @author: LiXiang
	 * @date: 2012-4-16 下午04:34:24
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/upload/{affixType}")
	public @ResponseBody String ajaxUploadAffix(@PathVariable("affixType") String affixType, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		int maxPostSize = 10 * 1024 * 1024;
		String message = affixService.uploadAffix(request, response, maxPostSize, affixType);
		return message;
	}

	/**
	 * 
	 * 通用下载
	 * @author: LiXiang
	 * @param affixItemId
	 * @param request
	 * @param response
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/download/{affixItemId}")
	public void AffixLoad(
		@PathVariable("affixItemId") String affixItemId,
		HttpServletRequest request,HttpServletResponse response,ModelMap map
		) throws Exception{
		
		
		// 文件下载的目录
		String path ="";
		try {
			String xmlPath = AffixService.class.getClassLoader().getResource("/sys_config.xml").getPath();
			Configuration config = ConfigUtil.getXmlConfig(xmlPath);
			path = config.getString("upload-url");
		} catch (Exception e) {
			throw new RuntimeException("读取配置文件有误！");
		}
		

		String affixpath ="";
		String affixname = "";
		try{
			SaleAffixItem affix =affixService.getAffixItemById(affixItemId); 
			affixpath = affix.getSALEAFFIXITEMFILEPATH();
			affixname = affix.getSALEAFFIXITEMFILENAME(); 
		}catch(Exception ex){
			throw new Exception("附件不存在!"); 
		}
		String filePath = path+affixpath;
		filePath = filePath.replace("\\", "/");
		try {
			InputStream is = new FileInputStream(filePath);// 输入流
			String disposition = "attachment;filename="
					+ URLEncoder.encode(affixname, "UTF-8");// 注意(1)
			response.setContentType("application/x-msdownload;charset=UTF-8");// 注意(2)
			response.setHeader("Content-disposition", disposition);

			byte[] buffer = new byte[1024];
			int len = -1;
			ServletOutputStream outputStream = response.getOutputStream();
			while ((len = is.read(buffer)) > 0) {
				outputStream.write(buffer, 0, len);
			}
			outputStream.close();
			is.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}	
